<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="utf-8">
    <title></title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="/static/frontend/lib/layui-v2.5.5/css/layui.css" media="all">
    <link rel="stylesheet" href="/static/frontend/css/public.css" media="all">
<!--    <link rel="stylesheet" href="/static/frontend/lib/jq-module/zyupload/zyupload-1.0.0.min.css">-->
    <style>
        body {
            background-color: #ffffff;
        }
    </style>
</head>
<body>
<form class="layui-form layuimini-form" autocomplete="off">
    <div class="layui-form-item">
        <label class="layui-form-label required">名称</label>
        <div class="layui-input-block">
            <input type="text" name="name" lay-verify="required|name" lay-reqtext="名称不能为空" placeholder="请输入名称" value="" class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label required">项目</label>
        <div class="layui-input-block">
            <select name="project" lay-verify="required" lay-search>
                <option value=""></option>
                {foreach $projects as $v}
                <option value="{$v.id}">{$v.name}</option>
                {/foreach}
            </select>
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label required">时间</label>
        <div class="layui-inline">
            <div class="layui-input-inline">
                <input type="text" name="startTime" class="layui-input" id="startTime" placeholder="预计开始" lay-verify="required">
            </div>
        </div>
        <div class="layui-inline">
            <div class="layui-input-inline">
                <input type="text" name="endTime" class="layui-input" id="endTime" placeholder="预计结束" lay-verify="required">
            </div>
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label required">优先级</label>
        <div class="layui-input-block">
            <select name="priority" lay-verify="required" lay-search>
                <option value="1">低</option>
                <option value="2">中</option>
                <option value="3">高</option>
            </select>
        </div>
    </div>
    <div class="layui-form-item layui-form-text">
        <label class="layui-form-label">详情描述</label>
        <div id="content" class="layui-input-block">

        </div>
    </div>
    <!--<div class="layui-form-item">
        <label class="layui-form-label">附件</label>
        <div class="layui-input-block">
            <div id="zyupload" class="zyupload"></div>
        </div>
    </div>-->
    <div class="layui-form-item">
        <div class="layui-input-block">
            <button class="layui-btn layui-btn-normal" lay-submit lay-filter="saveBtn">确认保存</button>
        </div>
    </div>
</form>
<script src="/static/frontend/lib/layui-v2.5.5/layui.js" charset="utf-8"></script>
<script src="/static/frontend/js/wangEditor.min.js"></script>
<script src="/static/frontend/lib/jquery-3.4.1/jquery-3.4.1.min.js"></script>
<!--<script src="/static/frontend/lib/jq-module/zyupload/zyupload-1.0.0.min.js"></script>-->
<script>
    layui.use(['form', 'layer', 'laydate'], function () {
        var form = layui.form,
            layer = layui.layer,
            $ = layui.$,
            laydate = layui.laydate;

        //开始时间选择器
        laydate.render({
            elem: '#startTime'
            ,type: 'datetime'
            ,trigger: 'click'
        });

        // 结束时间选择器
        laydate.render({
            elem: '#endTime'
            ,type: 'datetime'
            ,trigger: 'click'
        });

        /*// 附件上传
        let attachment = [];
        // 初始化插件
        $("#zyupload").zyUpload({
            width: "100%",                 // 宽度
            itemWidth: "140px",                 // 文件项的宽度
            itemHeight: "115px",                 // 文件项的高度
            url: "/frontend/requirement/upload-attachment",  // 上传文件的路径
            fileType: ["jpg", "jpeg", "png", "txt", "xls", "xlsx", "doc", "docx", "pdf", "zip"], // 上传文件的类型
            fileSize: 51200000,                // 上传文件的大小
            multiple: true,                    // 是否可以多个文件上传
            dragDrop: true,                    // 是否可以拖动上传文件
            tailor: true,                    // 是否可以裁剪图片
            del: true,                    // 是否可以删除文件
            finishDel: false,  				  // 是否在上传文件完成后删除预览
            /!* 外部获得的回调接口 *!/
            onSelect: function (selectFiles, allFiles) {    // 选择文件的回调方法  selectFile:当前选中的文件  allFiles:还没上传的全部文件
            },
            onDelete: function (file, files) {              // 删除一个文件的回调方法 file:当前删除的文件  files:删除之后的文件
            },
            onSuccess: function (file, response) {          // 文件上传成功的回调方法
                let res = JSON.parse(response);
                if (res.code === 0) {
                    // 上传成功
                    attachment.push(res.src);
                    layer.msg(res.msg, {
                        icon:1,
                        time: 1000
                    });
                } else if (res.code == 302) {
                    location.href = '/frontend/error/' + res.msg;
                } else {
                    layer.msg(res.msg, {icon: 2});
                }
            },
            onFailure: function (file, response) {          // 文件上传失败的回调方法
            },
            onComplete: function (response) {           	  // 上传完成的回调方法
            }
        });*/


        //监听提交
        form.on('submit(saveBtn)', function (data) {
            var postData = {
                'name': data.field.name,
                'project': data.field.project,
                'startTime': data.field.startTime,
                'endTime': data.field.endTime,
                'priority': data.field.priority,
                'content': editor.txt.html(),
                // 'attachment': attachment
            };
            $.post('/frontend/requirement/save', postData, function (res) {
                if (res.code == 0) {
                    // 添加成功
                    layer.msg(res.msg, {
                        icon:1,
                        time: 1000
                    },function () {
                        // 重新加载父页面
                        parent.location.reload();
                    });
                } else if (res.code == 302) {
                    location.href = '/frontend/error/' + res.msg;
                } else {
                    layer.msg(res.msg, {icon: 2});
                }
            }, 'json');
            return false;
        });

        // 自定义验证规则
        form.verify({
            name: function (value, item) {
                if (value.length > 20) {
                    return '标题不能超过20个字符';
                }
            }
        })
    });

    // 富文本编辑器
    const E = window.wangEditor;
    const editor = new E('#content');
    // 配置z-index
    editor.config.zIndex = 500;
    // 配置server接口地址
    editor.config.uploadImgServer = '/frontend/requirement/upload-img';
    editor.create();
</script>
</body>
</html>